package com.tm.media.login.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.utils.utils.Result;
import com.tm.media.login.service.IAdMenuService;
import com.tm.media.model.entity.AdMenu;
import com.tm.media.model.mapper.AdMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单权限Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Service
public class AdMenuServiceImpl extends ServiceImpl<AdMenuMapper, AdMenu> implements IAdMenuService {
    @Autowired
    private AdMenuMapper adMenuMapper;

    @Override
    public Result tree() {
        List<AdMenu> menuList = adMenuMapper.selectList(null);
        List<AdMenu> adMenuList = menuTree(menuList, 0);
        return Result.SUCCESS(adMenuList);
    }


    public List<AdMenu>menuTree(List<AdMenu>list, Integer pid){
        List<AdMenu>father=new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getParentId()==pid){
                List<AdMenu> son = menuTree(list, list.get(i).getMenuId());
                list.get(i).setAdMenuList(son);
                father.add(list.get(i));
            }
        }
        return father;
    }
    /**
     * 查询菜单权限
     *
     * @param menuId 菜单权限主键
     * @return 菜单权限
     */
    @Override
    public AdMenu selectAdMenuByMenuId(Long menuId)
    {
        return adMenuMapper.selectAdMenuByMenuId(menuId);
    }

    /**
     * 查询菜单权限列表
     *
     * @param adMenu 菜单权限
     * @return 菜单权限
     */
    @Override
    public List<AdMenu> selectAdMenuList(AdMenu adMenu)
    {
        List<AdMenu> adMenus = adMenuMapper.selectAdMenuList(adMenu);
        return adMenus;
    }

    /**
     * 新增菜单权限
     *
     * @param adMenu 菜单权限
     * @return 结果
     */
    @Override
    public int insertAdMenu(AdMenu adMenu)
    {
        return adMenuMapper.insertAdMenu(adMenu);
    }

    /**
     * 修改菜单权限
     *
     * @param adMenu 菜单权限
     * @return 结果
     */
    @Override
    public int updateAdMenu(AdMenu adMenu)
    {
        return adMenuMapper.updateAdMenu(adMenu);
    }

    /**
     * 批量删除菜单权限
     *
     * @param menuIds 需要删除的菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteAdMenuByMenuIds(Long[] menuIds)
    {
        return adMenuMapper.deleteAdMenuByMenuIds(menuIds);
    }

    /**
     * 删除菜单权限信息
     *
     * @param menuId 菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteAdMenuByMenuId(Long menuId)
    {
        return adMenuMapper.deleteAdMenuByMenuId(menuId);
    }
}
